package com.ghd.dataviz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ghd.dataviz.dummy.KpiInfo;
import com.ghd.dataviz.dummy.KpiSelector;
import com.ghd.dataviz.entity.RealtimeData;
import com.ghd.dataviz.mapper.RealtimeDataMapper;
import com.ghd.dataviz.param.KpiDatas;
import com.ghd.dataviz.service.IRealtimeDataService;
import com.ghd.dataviz.utils.KpiUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author guohd
 * @since 2020-03-08
 */
@Service
public class RealtimeDataServiceImpl extends ServiceImpl<RealtimeDataMapper, RealtimeData> implements IRealtimeDataService {

    @Override
    public KpiDatas getKpiDatasByKpis(List<KpiInfo> kpis) {
        List<RealtimeData> realtimeDataList = new ArrayList<>();
        // 获取正常指标数据（不带筛选条件）
        List<String> kpiNoSelectorList = kpis.stream().filter(kpiInfo -> CollectionUtils.isEmpty(kpiInfo.getTest()))
                .map(KpiInfo::getKey)
                .collect(Collectors.toList());
        if (CollectionUtils.isNotEmpty(kpiNoSelectorList)) {
            QueryWrapper<RealtimeData> queryWrapper = new QueryWrapper<>();
            queryWrapper.in("kpi", kpiNoSelectorList);
            queryWrapper.orderByAsc("kpi", "order_number");
            realtimeDataList.addAll(this.list(queryWrapper));
        }
        // 获取筛选条件数据
        List<RealtimeData> dataList =kpis.stream()
                .filter(kpiInfo -> CollectionUtils.isNotEmpty(kpiInfo.getTest()))
                .map(this::listData)
                .flatMap(List::stream)
                .collect(Collectors.toList());
        realtimeDataList.addAll(dataList);

        return KpiUtil.convert(realtimeDataList);
    }


    private List<RealtimeData> listData(KpiInfo kpiInfo) {
        QueryWrapper<RealtimeData> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("kpi", kpiInfo.getKey());
        for (KpiSelector selector : kpiInfo.getTest()) {
            switch (selector.getType()) {
                case EQ:
                    queryWrapper.eq(selector.getCol(), selector.getValue());
                    break;
                case GE:
                    queryWrapper.ge(selector.getCol(), selector.getValue());
                    break;
                case GT:
                    queryWrapper.gt(selector.getCol(), selector.getValue());
                    break;
                case LE:
                    queryWrapper.le(selector.getCol(), selector.getValue());
                    break;
                case LT:
                    queryWrapper.lt(selector.getCol(), selector.getValue());
                    break;
                case NE:
                    queryWrapper.ne(selector.getCol(), selector.getValue());
                    break;
                default:
                    break;
            }
        }
        queryWrapper.orderByAsc("kpi", "order_number");
        return list(queryWrapper);
    }

    @Override
    public KpiDatas getAllKpiDatas() {
        QueryWrapper<RealtimeData> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("kpi", "order_number");
        List<RealtimeData> list = this.baseMapper.selectList(queryWrapper);
        return KpiUtil.convert(list);
    }
}
